#!/usr/bin/env python
# -*- coding: utf-8 -*-


import os, os.path, sys

import tempfile
import unittest

from foxglove import log
from foxglove.cellstore import Tuckahoe

DASH = '-'

def makeData(key) :
    return DASH.join([key] * 32)
    # makeData()

class   TestTuckahoe(unittest.TestCase) :

    def setUp(self) :
        store_fd, store_path = tempfile.mkstemp(suffix='.index', prefix='tuckahoe_')
        os.close(store_fd)

        self.store_path = store_path
        # setUp()

    def tearDown(self) :
        if os.path.exists(self.store_path) :
            os.remove(self.store_path)
        # tearDown()

    def test_001(self) :
        tablet = Tuckahoe(self.store_path)

        num_records = 1024

        for x in range(num_records/2):
            key = '%032d' % (num_records - x)  # insert keys in reverse order
            data = makeData(key)
            tablet.insert(key, data)

        for x in range(num_records/2-1):
            key = '%032d' % x  # and now some in forward order
            data = makeData(key)
            tablet.insert(key, data)

        for x in range(num_records/2):
            key = '%032d' % (num_records - x)  # find keys in reverse order
            data = makeData(key)
            value = tablet.find(key)
            self.assertEqual(data, value)

        for x in range(num_records/2-1):
            key = '%032d' % x  # and now some in forward order
            data = makeData(key)
            value = tablet.find(key)
            self.assertEqual(data, value)

        del tablet
        # test_001()

    # class TestTuckahoe()

#######################################################################

def test_suite():
    suite = unittest.TestSuite()

    suite.addTest(unittest.makeSuite(TestTuckahoe))

    return suite

if  __name__ == '__main__' :
    #log.setLogPath('/dev/null')
    log.enableDebug()

    unittest.main()
